var app = getApp();
var util = require('../../utils/utils.js');
const db = wx.cloud.database({});
const _ = db.command

Page({

  /**
   * 页面的初始数据
   */
  data: {
    writeAllID: 0,
    _id: '',
    studentName: '',
    studentID: '',
    studentImg: '../../images/upfile.png',
    createTime: '',
    openid: '',

  },

  onLoad: function (options) {
    var that = this;
    that.setData({
      _id: app.globalData.userInfo._id,
    })
    db.collection('user').doc(app.globalData.userInfo._id).get({
      success: res => {
        that.setData({
          writeAllID: res.data.writeAllID,
        })
      }
    })
    db.collection('identify').where({
      _openid:app.globalData.userInfo._openid,
      writeAllID: _.or([1, 2]),
    }).get({
      success:res => {
        that.setData({
          studentName:res.data[0].studentName,
          studentID:res.data[0].studentID,
          studentImg:res.data[0].studentImg
        })
      }
    })


  },
  getStudentNameValue: function (e) {
    this.setData({
      studentName: e.detail.value
    });
  },
  getStudentIDValue: function (e) {
    this.setData({
      studentID: e.detail.value
    });
  },

  idc_face: function () {
     var self = this
     wx.chooseImage({
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          wx.showLoading({
            title: '上传中',
          });
          let filePath = res.tempFilePaths[0];
          const name = Math.random() * 1000000;
          const cloudPath = name + filePath.match(/\.[^.]+?$/)[0];
          wx.cloud.uploadFile({
            cloudPath, //云存储图片名字
            filePath, //临时路径
            success: res => {
              self.setData({
                studentImg: res.fileID, //云存储图片路径,可以把这个路径存到集合，要用的时候再取出来
              });
              fileID = res.fileID;
            },
            fail: e => {
              console.error('[上传图片] 失败：', e)
            },
            complete: () => {
              wx.hideLoading()
            }
          });
        }
      })
  },

  save: function () {
    var self = this;
    if (self.data.studentName == "" || self.data.studentName == 'None') {
      wx.showToast({
        title: '请输入姓名',
        icon: 'fail',
        duration: 2000
      })
      return;
    } else if (self.data.studentID == "") {
      wx.showToast({
        title: '请输入学号',
        icon: 'success',
        duration: 2000
      })
      return;
    }
    //把数据给云数据库
    const cont = db.collection('identify');
    cont.add({
      data: {
        studentName: self.data.studentName,
        studentID: self.data.studentID,
        studentImg: self.data.studentImg,
        creatTime: util.formatTime(new Date()),
        // flag:0,
        reason: '',
        writeAllID: 1 //提交认证，还未审核
      },
      success: function (res) {
        wx.showModal({
          title: '成功',
          content: '您已经登记成功',
          showCancel: false,
          showConfirm: false
        })
        wx.navigateBack({
          delta: 1,
        })
        db.collection('user').doc(self.data._id).update({
          data: {
            writeAllID: 1 //提交认证，还未审核
          }
        })
      }
    });



  },


})